package com.wswy.wzcx.api;

import android.support.annotation.WorkerThread;
import android.util.Base64;

import android.util.Log;
import com.blankj.utilcode.util.EncryptUtils;
import com.blankj.utilcode.util.FileIOUtils;
import com.blankj.utilcode.util.FileUtils;
import com.wswy.wzcx.utils.ImageUtils;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;

/**
 * Created by wangduheng26 on 2018/1/24.
 * wangduheng26@gmail.com
 */

public class Base64Util {

    @WorkerThread
    public static String convert(String filePath,int maxSize) {
        String encodedfile = null;
        File file=new File(filePath);
        if(file.exists() && file.isFile()){

            try {
                if (maxSize > 0 && file.length() > maxSize){
                    //超过指定文件长度，压缩
                    File tmp = ImageUtils.compress(filePath);
                    if (tmp.exists() && tmp.length() < maxSize){
                        file = tmp;
                    }
                }
            } catch (Throwable e) {
                e.printStackTrace();
            }

            byte[] bytes = FileIOUtils.readFile2BytesByStream(file);
            encodedfile= Base64.encodeToString(bytes, Base64.NO_WRAP);
        }

        return encodedfile;

    }


}
